<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<script>
    var demo = [
        {
            "value": "浙江",
            "children": [{
                "value": "杭州",
                "children": [{
                    "value": "西湖"
                }]
            }]
        },
        {
            "value": "四川",
            "children": [
                {
                    "value": "成都",
                    "children": [
                        {
                            "value": "锦里"
                        }, {
                            "value": "方所"
                        }
                    ]
                },
                {
                    "value": "阿坝",
                    "children": [
                        {
                            "value": "九寨沟"
                        }
                    ]
                }]
        }
    ];


    var table = [{
        "province": "四川",
        "city": "成都",
        "name": "锦里"
    }, {
        "province": "四川",
        "city": "成都",
        "name": "方所"
    }, {
        "province": "四川",
        "city": "阿坝",
        "name": "九寨沟"
    }];

    var keys=['province','city','name'];

    var transObject = function (tableData, keys) {
        var hashTable = {}, res = [];
        for (var i = 0; i < tableData.length; i++) {
            var arr = res, cur = hashTable;
            //debugger;

            for (let j = 0; j < keys.length; j++) {
                var key = keys[j], filed = tableData[i][key];
                //debugger;
                if (!cur[filed]) {
                    var pusher = {
                        value: filed
                    }, child;
                    if (j !== (keys.length - 1)) {
                        child = [];
                        pusher.children = child
                    }
                    cur[filed] = {$$pos: arr.push(pusher) - 1};
                    cur = cur[filed];
                    arr = child;
                } else {
                    cur = cur[filed];
                    arr = arr[cur.$$pos].children
                }
            }
        }
        return res;
    };
    console.dir(transObject(table, keys));

    function t(table, keys) {
        var hash={},res=[];

        for(var i=0;i<table.length;i++){
            var arr=res,curr=hash;

            for(var j=0;j<keys.length;j++){
                var key=keys[j],filed=table[i][key];
                console.log('filed:',filed);
                if(!curr[filed]){
                    var item={value:filed},child;
                    if(j!=keys.length-1){
                        child=[];
                        item.children=child;
                    }
                    arr.push(item);
                    curr[filed]={idx:arr.length-1};
                    curr=curr[filed];
                    arr=child;
                }else{
                    curr=curr[filed];
                    //console.log(curr);
                    arr=arr[curr.idx].children;

                }
            }
        }
        return res;
    }
    console.dir(t(table, keys));

</script>
</body>
</html>